/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.dialog;

import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import org.jxSwing.i18n.I18N;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.dialog
 * @date 17/05/2014 - 19:26:07
 */
public class Dialog extends JDialog {
	
	private static final long serialVersionUID = 3654442192148327319L;
	
	private boolean showExitQuestion = true;
	
	/**
	 * 
	 */
	public Dialog() {
	
		super();
		
		initGUI();
	}
	
	/**
	 * @param owner
	 */
	public Dialog(final Frame owner) {
	
		super(owner);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param modal
	 */
	public Dialog(final Frame owner, final boolean modal) {
	
		super(owner, modal);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param title
	 */
	public Dialog(final Frame owner, final String title) {
	
		super(owner, title);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param title
	 * @param modal
	 */
	public Dialog(final Frame owner, final String title, final boolean modal) {
	
		super(owner, title, modal);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param title
	 * @param modal
	 * @param gc
	 */
	public Dialog(final Frame owner, final String title, final boolean modal, final GraphicsConfiguration gc) {
	
		super(owner, title, modal, gc);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 */
	public Dialog(final java.awt.Dialog owner) {
	
		super(owner);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param modal
	 */
	public Dialog(final java.awt.Dialog owner, final boolean modal) {
	
		super(owner, modal);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param title
	 */
	public Dialog(final java.awt.Dialog owner, final String title) {
	
		super(owner, title);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param title
	 * @param modal
	 */
	public Dialog(final java.awt.Dialog owner, final String title, final boolean modal) {
	
		super(owner, title, modal);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param title
	 * @param modal
	 * @param gc
	 */
	public Dialog(final java.awt.Dialog owner, final String title, final boolean modal, final GraphicsConfiguration gc) {
	
		super(owner, title, modal, gc);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 */
	public Dialog(final Window owner) {
	
		super(owner);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param modalityType
	 */
	public Dialog(final Window owner, final ModalityType modalityType) {
	
		super(owner, modalityType);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param title
	 */
	public Dialog(final Window owner, final String title) {
	
		super(owner, title);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param title
	 * @param modalityType
	 */
	public Dialog(final Window owner, final String title, final ModalityType modalityType) {
	
		super(owner, title, modalityType);
		
		initGUI();
	}
	
	/**
	 * @param owner
	 * @param title
	 * @param modalityType
	 * @param gc
	 */
	public Dialog(final Window owner, final String title, final ModalityType modalityType, final GraphicsConfiguration gc) {
	
		super(owner, title, modalityType, gc);
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setSize(350, 350);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
		//
		final KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true);
		
		getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks, "esc");
		getRootPane().getActionMap().put("esc", new AbstractAction() {
			
			private static final long serialVersionUID = -2925160362044680532L;
			
			@Override
			public void actionPerformed(final ActionEvent actionEvent) {
			
				exitAction();
			}
		});
		
		//
		addWindowListener(new WindowAdapter() {
			
			@Override
			public void windowClosing(final WindowEvent windowEvent) {
			
				exitAction();
			}
		});
	}
	
	/**
	 * 
	 */
	public final void centralize() {
	
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	/**
	 * 
	 */
	public void exitAction() {
	
		//
		if(getDefaultCloseOperation() == DO_NOTHING_ON_CLOSE && showExitQuestion()) {
			
			//
			if(JOptionPane.showConfirmDialog(this, I18N.component().reallyCloseTheScreen(getTitle() != null ? getTitle().trim() : ""), null, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
				
				dispose();
			}
			
		}else {
			
			//
			switch(getDefaultCloseOperation()) {
			
				case HIDE_ON_CLOSE:
					
					setVisible(false);
					break;
				
				default:
					
					dispose();
					break;
			}
		}
	}
	
	/**
	 * @return the showExitQuestion
	 */
	public final boolean showExitQuestion() {
	
		return showExitQuestion;
	}
	
	/**
	 * @param showExitQuestion
	 *        the showExitQuestion to set
	 */
	public final void showExitQuestion(final boolean showExitQuestion) {
	
		this.showExitQuestion = showExitQuestion;
	}
}
